import cgi
import datetime
import urllib
import webapp2

import jinja2
import os

dir = os.path.dirname(__file__)
filename = os.path.join(dir, '../view')
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(filename))

from google.appengine.ext import db
from google.appengine.api import users

from model.Greeting import Greeting
from model.Person import Person
from model.User import User

def guestbook_key(guestbook_name=None):
  """Constructs a datastore key for a Guestbook entity with guestbook_name."""
  return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')

class MainPage(webapp2.RequestHandler):
    def get(self):

        #Prueba
		#m3 = Person(3, fb_uid=515344920, gender='male', searching='female')
		#m3.ranking = [7,8,5,6]
		#m3.put()
        #m1 = Person(fb_uid=1, gender='male', searching='female')
        #m2 = Person(fb_uid=2, gender='male', searching='female')
        #m3 = Person(fb_uid=3, gender='male', searching='female')

        #w1 = Person(fb_uid=4, gender='female', searching='male')
        #w2 = Person(fb_uid=5, gender='female', searching='male')
        #w3 = Person(fb_uid=6, gender='female', searching='male')

        #w1.put()
        #w2.put()
        #w3.put()

        #m1.ranking = [w1.key(), w2.key(), w3.key()]

        #m1.put()
        #m2.put()
        #m3.put()

        #fin de prueba

		
	# obtengo toda la gente que esta en el match
        people_query = Person.all()

        people = people_query.fetch(4)
		
        guestbook_name=self.request.get('guestbook_name')
        greetings_query = Greeting.all().ancestor(
            guestbook_key(guestbook_name)).order('-date')
        greetings = greetings_query.fetch(10)

        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'

        template_values = {
            'greetings': greetings,
            'url': url,
            'url_linktext': url_linktext,
			'people': people,
        }

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))